<?php
class siteModel extends hopeCmsModel
{
    //数据
    private $_data;

	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
    {
        return '{site}';
    }

    public function primaryKey()
    {
        return 'site_id';
    }

    /**
     * 获取站点的配置信息
     * 
     * @param int $menuId
     */
    public function getSiteInfo($site_id = 1)
    {
    	$site = $this->findByPk($site_id);
        $setting = unserialize($site['site_setting']);
        unset($site['site_setting']);
        return array_merge($setting, $site);
    }

    /**
     * 获取当前站点名
     */
    public function getCurrentSiteName($site_id = 1)
    {
        $site = $this->findByAttribute('site_name', 'site_id='.$site_id);
        return isset($site['site_name']) ? $site['site_name'] : '';
    }

    /**
     * 获取所有站点，只获取id和名字
     */
    public function getSite()
    {
        return $this->findsByAttribute('site_id, site_name');
    }

    /**
     * 添加站点前置处理
     */
    public function beforeSave()
    {
        $this->_data['site_name']        = trim(htmlspecialchars($_POST['site_name']));
        $this->_data['site_domain']      = trim(htmlspecialchars($_POST['site_domain']));
        $this->_data['site_title']       = trim(htmlspecialchars($_POST['site_title']));
        $this->_data['site_keywords']    = trim(htmlspecialchars($_POST['site_keywords']));
        $this->_data['site_description'] = trim(htmlspecialchars($_POST['site_description']));
        $this->_data['site_theme']       = trim(htmlspecialchars($_POST['site_theme']));
        $this->_data['site_icp']         = trim(htmlspecialchars($_POST['site_icp']));
        unset(
            $_POST['token'],            $_POST['site_name'],
            $_POST['site_domain'],      $_POST['site_title'],
            $_POST['site_keywords'],    $_POST['site_description'],
            $_POST['site_theme'],       $_POST['site_icp'],     $_POST['site_id']
        );
        $this->_data['site_setting']     = serialize($_POST);
        unset($_POST);
        $this->setData($this->_data);
        return true;
    }

    public function scopes()
    {
        return array(
            'recently' => array(
                'order' => '{0}',
                'limit' => '{1}'
            ),
        );
    }

}
?>